How to generate a number in arbitrary range using random()={0..1} preserving uniformness and density?
Posted
by
psihodelia
on Stack Overflow
See other posts from Stack Overflow
or by psihodelia
Published on 2011-11-05T10:32:05Z
Indexed on
2011/11/12
9:50 UTC
Read the original article
Hit count: 276
Generate a random number in range [x..y] where x and y are any arbitrary floating point numbers. Use function random(), which returns a random floating point number in range [0..1] from P uniformly distributed numbers (call it "density"). Uniform distribution must be preserved and P must be scaled as well.
I think, there is no easy solution for such problem. To simplify it a bit, I ask you how to generate a number in interval [-0.5 .. 0.5], then in [0 .. 2], then in [-2 .. 0], preserving uniformness and density? Thus, for [0 .. 2] it must generate a random number from P*2 uniformly distributed numbers.
The obvious simple solution random() * (x - y) + y
will generate not all possible numbers because of the lower density for all abs(x-y)>1.0
cases. Many possible values will be missed. Remember, that random() returns only a number from P possible numbers. Then, if you multiply such number by Q, it will give you only one of P possible values, scaled by Q, but you have to scale density P by Q as well.
© Stack Overflow or respective owner